//
//  SGLoginViewController.m
//  SemiGroup
//
//  Created by 陈无忌 on 12/9/15.
//  Copyright © 2015 陈无忌. All rights reserved.
//

#import "SGLoginViewController.h"
#import "SGRegisterViewController.h"

#define kCenterOffsetY 25

@interface SGLoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIView *operateView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *operationViewConstraint;

@property (nonatomic) CGFloat originalY;

@end

@implementation SGLoginViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.originalY = self.operateView.frame.origin.y;
    [self configTextField];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillHideNotification object:nil];
    // Do any additional setup after loading the view.
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)configTextField{
    self.phoneNumberTextField.delegate = self;
    self.passwordTextField.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Notification

- (void)keyboardChange:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardEndFrame;
    
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];
    
    //adjust ChatTableView's height
    if (notification.name == UIKeyboardWillShowNotification) {
        CGFloat nowY = kScreenHeight - 140 - keyboardEndFrame.size.height;
//        CGFloat originalY = self.operateView.frame.origin.y;
        if (self.originalY > nowY) {
            self.operationViewConstraint.constant = kCenterOffsetY - (self.originalY - nowY);
        }
//        self.operationViewConstraint.constant = keyboardEndFrame.size.height;
    }else{
        self.operationViewConstraint.constant = kCenterOffsetY;
    }
    [self.view layoutIfNeeded];
    [UIView commitAnimations];
}

#pragma mark - Custom IBAction

- (IBAction)loginClick:(UIButton *)sender {
    
}

- (IBAction)forgetPasswordClick:(UIButton *)sender {
    
}

- (IBAction)goRegisterClick:(UIButton *)sender {
    SGRegisterViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SGRegisterViewController"];
    [self.navigationController pushViewController:targetVC animated:YES];
}

- (IBAction)lookBySelfClick:(UIButton *)sender {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Text Field delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - View Touch
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self.phoneNumberTextField isExclusiveTouch]) {
        [self.phoneNumberTextField resignFirstResponder];
    }
    if (![self.passwordTextField isExclusiveTouch]) {
        [self.passwordTextField resignFirstResponder];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
